import cv2
import requests
from project_final.dao.user_dao import insert_user, get_user_face


def detect_api(img):
    url = "https://api-cn.faceplusplus.com/facepp/v3/detect"
    request_data = {'api_key': 'OVHDatOz2oMDzUPFt72QMvJydexI0gPN',
                    'api_secret': 'NTJRr0ZloCFf5NnvjSTgjTzwRZCGwtj1',
                    }
    files = {'image_file': ('user.png', img, 'image/png')}
    ret = requests.post(url=url, data=request_data, files=files).json()
    return ret


def compare_api(login_img, register_img):
    url = "https://api-cn.faceplusplus.com/facepp/v3/compare"
    request_data = {'api_key': 'OVHDatOz2oMDzUPFt72QMvJydexI0gPN',
                    'api_secret': 'NTJRr0ZloCFf5NnvjSTgjTzwRZCGwtj1',
                    }
    files = {'image_file1': ('user-log.png', login_img, 'image/png'),
             'image_file2': ('user_regis.png', register_img, 'image/png')}
    ret = requests.post(url=url, data=request_data, files=files).json()
    return ret


def use_camera_register(username):
    # 打开摄像头
    capture = cv2.VideoCapture(0)
    while True:
        # 读取视频图片`
        ret, frame = capture.read()
        cv2.imshow('video', frame)  # 显示
        if cv2.waitKey(20) & 0xFF == ord('q'):
            # 保存图片
            cv2.imwrite("register_img/test.png", frame)
            f = open("register_img/test.png", "rb")
            # fp = f.read()
            ret = detect_api(f)
            print(ret)
            f.close()
            # 如果获取脸的数量不为0，则保存至数据库
            if ret['face_num'] != 0:
                print(username)
                insert_user(username)
                # 关闭窗口
                capture.release()
                cv2.destroyAllWindows()
                return "注册成功"
            else:
                capture.release()
                cv2.destroyAllWindows()
                return "请正确拍摄人像"


def use_camera_login(username):
    # 打开摄像头
    capture = cv2.VideoCapture(0)
    while True:
        # 读取视频图片`
        ret, frame = capture.read()
        cv2.imshow('video', frame)  # 显示
        if cv2.waitKey(20) & 0xFF == ord('q'):
            # 保存图片
            cv2.imwrite("login_img/test.png", frame)
            login_img = open("login_img/test.png", "rb")
            print(login_img)
            face_flag = get_user_face(username)
            if face_flag == 1:
                regis_img = open('login_img/quchu1.png', "rb")
                ret = compare_api(login_img, regis_img)
                print(ret)
                # 如果获取脸的数量不为0，则保存至数据库
                confidence = ret['confidence']
                if confidence is not None:
                    if confidence > 90:
                        print("对比成功")
                        # 关闭窗口
                        capture.release()
                        cv2.destroyAllWindows()
                        return 1
                    else:
                        # 关闭窗口
                        capture.release()
                        cv2.destroyAllWindows()
                        return 2
                else:
                    capture.release()
                    cv2.destroyAllWindows()
                    return 3
            else:
                capture.release()
                cv2.destroyAllWindows()
                return 4
# if __name__ == '__main__':
#     use_camera_login("bbb")
